package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/permission")
public class PermissionController {
    private PermissionService permissionService;
    private UserController userController;
    public PermissionController(PermissionService permissionService, UserController userController) {
        this.permissionService = permissionService;
        this.userController = userController;
    }
    @GetMapping("/list")
    public List<Permission> list() {
        return permissionService.list();
    }
    @GetMapping("/listByPage")
    public IPage<Permission> listByPage(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize
    ){
        return permissionService.page(new Page<>(pageNum,pageSize));
    }
    @GetMapping("/listByPageWithCondition")
    public IPage<Permission> listByPageWithCondition(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) String keyword
    ){
        Page<Permission> page = new Page<>(pageNum,pageSize);
        return permissionService.getPermissionsByPageWithCondition(page,keyword);
    }
    @PostMapping("/create")
    public boolean create(@RequestBody Permission permission) {
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_name", permission.getPermName())) > 0) {
            return false;
        }
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_key", permission.getPermKey())) > 0) {
            return false;
        }
        return permissionService.save(permission);
    }
    //删除权限
    @PostMapping("/delete")
    public boolean delete(@RequestBody Permission permission) {
        return permissionService.removeById(permission);
    }
    @PostMapping("/delete/ids")
    private boolean delete(@RequestBody List<Long> ids) {
        return permissionService.removeByIds(ids);
    }

    /***
     * 根据用户获取权限列表
     * @param userId
     * @return 权限列表
     */
    @GetMapping("/listPermissionByUserId")
    public List<Permission> getPermissionByUserId(@RequestParam Long userId) {
        //查找用户是否存在
        if (permissionService.getById(userId) == null) {
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByUserId(userId);
    }
    /***
     * 根据角色获取权限列表
     * @param roleId
     * @return 权限列表
     */
    @GetMapping("/listPermissionByRoleId")
    public List<Permission> getPermissionByRoleId(@RequestParam Long roleId) {
        //角色是否存在的业务逻辑，在完成角色模块时再行添加
        if (permissionService.getById(roleId) == null) {
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionsByRoleId(roleId);
    }
    @GetMapping("/listPermissionIdsByUserId")
    public Set<String> getPermissionIdsByUserId(@RequestParam Long userId) {
        //查找角色是否存在
        if (permissionService.getById(userId) == null) {
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionsByUserIdSet(Set.of(userId));
    }
    @GetMapping("/listPermissionIdsByUserId")
    public Set<String> getPermissionIdsByRoleId(@RequestParam Long roleId) {
        //角色是否存在的业务逻辑，在完成角色模块时再行添加
        return permissionService.getPermissionsByUserIdSet(Set.of(roleId));
    }
}